<template>
	<view class="">
		<u-navbar backIconColor="#ffffff" titleColor="#ffffff" :title="title"
			:background="{background: 'var(--mainColor)'}">
			<view class="flex align-center u-m-r-40" slot='right' v-if="type == 'edit'" @click="del">
				<text class="u-font-26 text-white">删除</text>
			</view>
		</u-navbar>
		<view class="u-p-x-30 bg-white">
			<u-form :model="form" ref="uForm" labelWidth="130">
				<u-form-item prop="name" label="收货人">
					<u-input v-model="form.name" placeholder="请输入收货人姓名" />
				</u-form-item>
				<u-form-item prop="phone" label="手机号码">
					<u-input v-model="form.phone" placeholder="请输入收货人手机号码" />
				</u-form-item>
				<u-form-item prop="region" label="所在地址">
					<u-input disabled v-model="form.region" type="select" placeholder="请选择地区" @click="show = true" />
				</u-form-item>
				<u-form-item prop="detail" label="详细地址">
					<u-input v-model="form.detail" type="textarea" placeholder="请输入详细地址信息，如道路、门牌号、小区、楼栋号、单元室等" />
				</u-form-item>
			</u-form>
		</view>
		<view class="u-p-x-30 u-m-t-20 bg-white">
			<u-form-item label="默认地址" labelWidth="120">
				<u-switch slot="right" v-model="isdefault"></u-switch>
			</u-form-item>
		</view>
		<u-picker v-model="show" mode="region" @confirm="confirm"></u-picker>
		<view class="u-m-t-60">
			<dy-button size="default" width="576rpx" @click="sure">保存</dy-button>
		</view>
	</view>
</template>

<script>
	import {
		BaseCenterApi
	} from '@/api/serves.js'
	import amap from '@/common/map/amap-wx.130.js';
	export default {
		data() {
			return {
				show: false,
				title: '添加地址',
				type: 'edit',
				addressId: '',
				form: {
					name: '',
					phone: '',
					region: '',
					detail: '',
					isdefault: 1,
					lng: null,
					lat: null,
				},
				isdefault: true,
				rules: {
					phone: [{
							required: true,
							message: '请输入手机号码',
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['blur'],
						},
						{
							validator: (rule, value, callback) => {
								if (!this.$u.test.mobile(value)) {
									callback(new Error("请输入正确的手机号码"));
								} else {
									callback();
								}
							},
						},
					],
					name: [{
						required: true,
						message: '请输入姓名',
						trigger: ['blur', 'change'],
					}],
					region: [{
						required: true,
						message: '请选择所在地',
						trigger: ['blur', 'change'],
					}],
					detail: [{
						required: true,
						message: '请输入详细地址',
						trigger: ['blur', 'change'],
					}],
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(options) {
			this.type = JSON.parse(options.info).type
			this.addressId = JSON.parse(options.info).id
			this.title = this.type == 'edit' ? '编辑地址' : '添加地址'
			if (this.addressId) this._adressDetail()
		},
		methods: {
			confirm(e) {
				console.log(e);
				this.form.region = `${e.province.label},${e.city.label},${e.area.label}`
			},
			_adressDetail() {
				BaseCenterApi.adressdetail({
					id: this.addressId
				}).then(res => {
					console.log('地址详细信息', res);
					if (res.code == 1) {
						// this._addressList()
						let result = res.data.detail
						this.form.name = result.name
						this.form.phone = result.phone
						this.form.detail = result.detail
						this.form.name = result.name
						this.form.region = res.data.rArea.join(',')
						this.isdefault = res.data.detail.isdefault == '1' ? true : false
					}
				})
			},
			getAddress() {
				var that = this;
				var keywords = this.form.region + this.form.detail;
				console.log(keywords);
				uni.getLocation({
					type: 'wgs84',
					geocode: true, //设置该参数为true可直接获取经纬度及城市信息
					success: function(res) {
						console.log(res)
						that.form.lng = res.longitude
						that.form.lat = res.latitude
						if (that.type == 'add') that._addressAdd()
						if (that.type == 'edit') that._addressEdit()
					},
					fail: function(err) {
						console.log(err)
						uni.showToast({
							title: '获取位置信息失败，请授权位置信息',
							icon: 'none'
						});
					}
				})
				// var myAmapFun = new amap.AMapWX({
				// 	key: '2d7598b3c967379ffd30b5ac841e6cbe'
				// });
				// #ifdef APP-PLUS
					
				// #endif
				// #ifndef APP-PLUS
					/**
					 * @param "作者: D"
					 * @param "时间: 2023.11.2  9.30"
					 * @description  "获取经纬度"
					 */
					// myAmapFun.getRegeo({
					// 	success: function(data) {
					// 		console.log('success',data[0]);
					// 		if (data[0]) {
					// 			let {
					// 				longitude,
					// 				latitude
					// 			} = data[0]
					// 			that.form.lng = longitude
					// 			that.form.lat = latitude
					// 		} else {
					// 			that.form.lng = null
					// 			that.form.lat = null
					// 		}
					// 		if (that.type == 'add') that._addressAdd()
					// 		if (that.type == 'edit') that._addressEdit()
					// 	},
					// 	fail:(err)=>{
					// 		console.log(err);
					// 		uni.showToast({
					// 			title: '获取地址失败，将导致部分功能不可用',
					// 			icon: 'none'
					// 		});
					// 	}
					// })
					
				// #endif
				
			},
			sure() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.getAddress()
					}
				});
			},
			_addressAdd() {
				this.form.isdefault = this.isdefault ? 1 : 0
				console.log('this.for111m', this.form);
				this.$ShowLoading()
				BaseCenterApi.addressadd(this.form).then(res => {
					this.$HideLoading()
					console.log('添加地址', res);
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack()
						}, 500)
					} else {
						this.$u.toast(res.msg)
					}
				})
			},
			_addressEdit() {
				this.form.isdefault = this.isdefault ? 1 : 0
				this.form.id = this.addressId
				console.log('this.form', this.form);
				this.$ShowLoading()
				BaseCenterApi.adressEdit(this.form).then(res => {
					this.$HideLoading()
					console.log('编辑地址', res);
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack()
						}, 500)
					} else {
						this.$u.toast(res.msg)
					}
				})
			},
			del() {
				uni.showModal({
					title: '提示',
					content: '确定要删除吗?',
					success: (res) => {
						if (res.confirm) {
							BaseCenterApi.adressDel({
								id: this.addressId
							}).then(res => {
								this.$HideLoading()
								console.log('设置默认地址', res);
								if (res.code == 1) {
									uni.navigateBack()
								} else {
									this.$u.toast(res.msg)
								}
							})
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>